package com.wzh.borrow.service.impl;

import com.wzh.common.utils.JwtUtil;
import com.wzh.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.borrow.dao.UserDao;
import com.wzh.borrow.entity.UserEntity;
import com.wzh.borrow.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R login(Map<String, Object> params) {
        String tel = (String) params.get("tel");
        String pwd = (String) params.get("pwd");
        UserEntity userEntity = baseMapper.selectById(tel);
        if (userEntity!=null) {
            if (userEntity.getPwd().equals(pwd)){
                String token = jwtUtil.createJWT(userEntity.getTel().toString(), userEntity.getName(), "admin");
                return R.ok("登陆成功").put("token",token);
            }
            return R.error("用户名或密码错误");
        }
        return R.error("用户名或密码错误");
    }

}